* David Sturrock
* June 2020
* This file analyses the outputs from the fortran code *

	* set globals *

global obs = 2848

	* setup *
clear all
set maxvar 10000
set more off


**************************************************
***	Get weights and other vars from main file  ***
**************************************************

	clear
	use "$savedata\ids_firstonly.dta"
	merge 1:1 idauniq wave using "$savedata\wave1-7base.dta", keepusing(wgt)
	assert _m != 1
	keep if _m == 3
	drop _m
	bys idauniq (wave) : gen obs = _n
	keep id idauniq wgt obs wave
	save "$temp\weights.dta", replace


************************************
*** 	Make results figs data	 ***
************************************

	clear 
	use "$simMasterPath\allRunsMicroData"

	* only need one line per observation *
	keep if time  == 1

	merge m:1 id using "$temp\weights.dta"
	assert _m ==3
	drop _m
	* keep first observation of each individual *
	keep if obs == 1

	** Fig 6a 
	preserve
	keep if objective == 1 & addMinLoad == 0 & housing == 0 & unscaled == 0 & annuityDiscreteChoice == 0
	drop objective addMinLoad housing unscaled
	collapse (mean) annProp [pw=wgt], by(beta gamma)
	replace  beta = 10*beta
	replace gamma = 10*gamma
	reshape wide annProp, i(beta) j(gamma)
	export excel using "$output\Fig6a.xls", sheetreplace firstrow(variables) sheet("data")
	restore
	
	
	** Fig 6b
	preserve
	keep if objective == 0 & addMinLoad == 0 & housing == 0 & unscaled == 0 & annuityDiscreteChoice == 0
	drop objective addMinLoad housing unscaled
	collapse (mean) annProp [pw=wgt], by(beta gamma)
	replace  beta = 10*beta
	replace gamma = 10*gamma
	reshape wide annProp, i(beta) j(gamma)
	export excel using "$output\Fig6b.xls", sheetreplace firstrow(variables) sheet("data")
	restore
	
	** Fig 7a
	preserve
	keep if objective == 1 & addMinLoad == 0 & housing == 0 & unscaled == 0 & annuityDiscreteChoice == 0
	drop objective addMinLoad housing unscaled
	collapse (mean) annProp [pw=wgt], by(beta gamma)
	replace  beta = 10*beta
	replace gamma = 10*gamma
	reshape wide annProp, i(beta) j(gamma)
	export excel using "$output\Fig7a.xls", sheetreplace firstrow(variables) sheet("data")
	restore

	** Fig 7b
	preserve
	keep if objective == 1 & addMinLoad == 1 & housing == 0 & unscaled == 0 & annuityDiscreteChoice == 0
	drop objective addMinLoad housing unscaled
	collapse (mean) annProp [pw=wgt], by(beta gamma)
	replace  beta = 10*beta
	replace gamma = 10*gamma
	reshape wide annProp, i(beta) j(gamma)
	export excel using "$output\Fig7b.xls", sheetreplace firstrow(variables) sheet("data")
	restore
	
	** Fig 9a
	preserve
	keep if objective == 1 & addMinLoad == 0 & housing == 0 & unscaled == 1 & annuityDiscreteChoice == 0
	drop objective addMinLoad housing unscaled
	collapse (mean) annProp [pw=wgt], by(beta gamma)
	replace  beta = 10*beta
	replace gamma = 10*gamma
	reshape wide annProp, i(beta) j(gamma)
	export excel using "$output\Fig9a.xls", sheetreplace firstrow(variables) sheet("data")
	restore
	
	** Fig 9b
	preserve
	keep if objective == 0 & addMinLoad == 0 & housing == 0 & unscaled == 1 & annuityDiscreteChoice == 0
	drop objective addMinLoad housing unscaled
	collapse (mean) annProp [pw=wgt], by(beta gamma)
	replace  beta = 10*beta
	replace gamma = 10*gamma
	reshape wide annProp, i(beta) j(gamma)
	export excel using "$output\Fig9b.xls", sheetreplace firstrow(variables) sheet("data")
	restore


****************************************************
*** 				Table 5		 				 ***
****************************************************

	clear 
	use "$simMasterPath\allRunsMicroData"
	
	* only need one line per observation *
	keep if time  == 1
	
	* merge in weights *
	merge m:1 id using "$temp\weights.dta"
	assert _m ==3
	drop _m
	
	* merge in info on 0%, 50%, 100% answers *
	merge m:1 idauniq wave using "$temp\model_inputs.dta", keepusing(says_0 says_50 says_100)
	assert _m != 1
	drop if _m == 2
	drop _m	
	
	* add in initial assets *
	merge m:1 id using "$savedata\fortran_inputs.dta", keepusing(assets)
	assert _m ==3
	drop _m
	
	* keep first observation of each individual *
	keep if obs == 1

	* keep results for one parameter combination for various runs *
	* in each case, show results for objective (scaled), subjective (scaled), objective (scaled) with loading
	keep if round(beta,0.001) == 0.98 & gamma == 3
	gen col = .
	replace col = 1 if objective == 1 & addMinLoad == 0 & unscaled == 0
	replace col = 2 if objective == 0 & addMinLoad == 0 & unscaled == 0
	replace col = 3 if objective == 1 & addMinLoad == 1 & unscaled == 0
	drop if col == .
	
	* 0) Baseline: results from main paper specification *
	preserve 	
	keep if annuityDiscreteChoice == 0 & housing  == 0
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 0
	reshape wide annProp , i(row) j(col)
	save "$temp\row0.dta", replace
	restore
	
	* 1) no 100s *
	preserve 	
	keep if annuityDiscreteChoice == 0 & says_100 == 0  & housing  == 0
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 1
	reshape wide annProp , i(row) j(col)
	save "$temp\row1.dta", replace
	restore
	
	* 2) no 0s *
	preserve 	
	keep if annuityDiscreteChoice == 0 & says_0 == 0  & housing  == 0
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 2
	reshape wide annProp , i(row) j(col)
	save "$temp\row2.dta", replace
	restore
	
	* 3) no 50s *
	preserve 	
	keep if annuityDiscreteChoice == 0 & says_50 == 0 & housing  == 0
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 3
	reshape wide annProp , i(row) j(col)
	save "$temp\row3.dta", replace
	restore
		
	* 4) no 0s, 50s, or 100s *
	preserve 	
	keep if annuityDiscreteChoice == 0 & says_0 == 0 & says_50 == 0 & says_100 == 0 & housing  == 0
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 4
	reshape wide annProp , i(row) j(col)
	save "$temp\row4.dta", replace
	restore
	
	* 5) No weighting *
	preserve 	
	keep if annuityDiscreteChoice == 0 & housing  == 0
	collapse (mean) annProp, by(col)
	gen row = 5
	reshape wide annProp , i(row) j(col)
	save "$temp\row5.dta", replace
	restore
		
	* 6) discrete choice *
	preserve 	
	keep if annuityDiscreteChoice == 1  & housing  == 0
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 6
	reshape wide annProp , i(row) j(col)
	save "$temp\row6.dta", replace
	restore
	
	* 7) wealth weighted *
	preserve 	
	keep if annuityDiscreteChoice == 0 & housing  == 0
	gen annWlth = annProp*assets
	collapse (mean) annWlth assets [pw = wgt], by(col)
	gen annProp = annWlth/assets
	drop annWlth assets
	gen row = 7
	reshape wide annProp , i(row) j(col)
	save "$temp\row7.dta", replace
	restore
	
	* 8) With housing flow in utility 
	preserve 	
	keep if annuityDiscreteChoice == 0 & housing  == 1
	collapse (mean) annProp [pw = wgt], by(col)
	gen row = 8
	reshape wide annProp , i(row) j(col)
	save "$temp\row8.dta", replace
	restore
	
	clear
	* Append together 
	forv r = 0(1)8 {
	append using "$temp\row`r'.dta"
	}	
	replace annProp1 = round(annProp1*100,1)
	replace annProp2 = round(annProp2*100,1)
	replace annProp3 = round(annProp3*100,1)
	dataout, save("$output\Table5.tex") tex replace

	